package com.yang.controller;

import com.yang.entity.NoteThings;
import com.yang.service.NoteThingsService;
import com.yang.utils.UtilsFactory;
import com.yang.utils.info.NoteResponseInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 事件表(NoteThings)表控制层
 *
 * @author yangdacheng
 * @since 2022-03-02 19:11:03
 */
@RestController
@RequestMapping("noteThings")
@Slf4j
public class NoteThingsController {
    /**
     * 服务对象
     */
    @Resource
    private NoteThingsService noteThingsService;
    @Resource
    private UtilsFactory utilsFactory;
    @Resource
    private RedisTemplate redisTemplate;


    /**
     * 修改用户事件
     * @param noteThings
     * @param start
     * @param end
     * @return
     */
    @PutMapping("updateThings")
    public NoteResponseInfo updThings(NoteThings noteThings, String start, String end,Boolean isStart,Boolean isEnd){
        log.info("====》"+noteThings.toString()+"---->"+start+"---=>"+end+"===="+isStart.toString()+"==="+isEnd.toString());
        NoteResponseInfo noteResponseInfo = new NoteResponseInfo();
        if (isStart){
            noteThings.setStartTime(start);
        }else{
            noteThings.setStartTime(utilsFactory.dateFormatStr(utilsFactory.interceptTime(start)));
        }
        if (isEnd){
            noteThings.setEndTime(end); //设置事件对象
        }else {
            noteThings.setEndTime(utilsFactory.dateFormatStr(utilsFactory.interceptTime(end)));
        }


        Boolean updResult = noteThingsService.updateNoteThings(noteThings);
        if (updResult){
            noteResponseInfo.setNoteCode(200).setNoteMessage("修改成功").setNoteClass(null);
        }else{
            noteResponseInfo.setNoteCode(292).setNoteMessage("修改失败").setNoteClass(null);
        }
        return noteResponseInfo;
    }

    /**
     * 用户事件打卡
     * @param thingsID
     * @return
     */
    @PutMapping("punchCard")
    public NoteResponseInfo PunchCard(String thingsID){
        NoteResponseInfo noteResponseInfo = new NoteResponseInfo();
        NoteThings noteThings = noteThingsService.queryThingsByID(thingsID);
        noteThings.setIsPunchCard(1);  //打卡操作
        Boolean punchResult = noteThingsService.updateNoteThings(noteThings);
        if (punchResult){
            noteResponseInfo.setNoteCode(200).setNoteMessage("打卡成功").setNoteClass(null);
        }else{
            noteResponseInfo.setNoteCode(293).setNoteMessage("打卡失败").setNoteClass(null);
        }
        return noteResponseInfo;
    }

    /**
     * 获取所有的用户事件
     * @return
     */
    @GetMapping("/queryThingsType")
    public NoteResponseInfo queryThingsType(){
        List thingsTypes = redisTemplate.opsForList().range("thingsTypes", 0, -1);
        NoteResponseInfo noteResponseInfo = new NoteResponseInfo();
        if (!thingsTypes.isEmpty()){
            noteResponseInfo.setNoteCode(200).setNoteMessage("获取用户事件类型成功").setNoteClass(thingsTypes);
        }
        return noteResponseInfo;
    }

}